package com.qinggeng.project.api.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.common.utils.spring.SpringUtils;
import com.qinggeng.common.utils.text.Convert;
import com.qinggeng.project.system.domain.OrdersGoods;
import com.qinggeng.project.system.domain.UserWishGift;
import lombok.Data;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.List;

/**
 * 订单详情商品信息
 */
@Data
public class AddOrdersInfoGoodsDto {

    //购物车ID
    private Long cartId;

    //商品ID
    private Long goodsId;

    //订单商品ID
    private Long ordersGoodsId;

    //商品名称
    private String goodsName;

    //商品图片
    private String goodsImg;

    //商品购买数量
    private Integer goodsNum = 1;

    //商品价格
    private BigDecimal goodsPrice;

    //拼团价格
    private BigDecimal groupPrice;

    //商品属性
    private String attributes;

    //是否评价
    private Boolean isEvaluate;

    //商品提成百分比
    private String percent;

    //商品进价
    private BigDecimal buyPrice;

    //退款状态
    private  Integer singleItemRefund;

    //商品总优惠价格
    private BigDecimal discountPrice;

    //优惠状态 1优惠 0未优惠
    private String discountStatus;

    //骑手Id
    private Long courierId;

    private List<UserWishGift> goodsGiftList;

    public AddOrdersInfoGoodsDto(){}

    //商品进价
    private BigDecimal vipPrice;

    public AddOrdersInfoGoodsDto(GoodsInfoDto goodsInfoDto, Integer goodsNum) {
        this.goodsId = goodsInfoDto.getGoodsId();
        this.goodsName = goodsInfoDto.getGoodsName();
        this.goodsImg = goodsInfoDto.getGoodsImg();
        this.goodsPrice = goodsInfoDto.getGoodsPrice();
        this.groupPrice=goodsInfoDto.getGroupPrice();
        this.attributes = goodsInfoDto.getAttributes();
        this.percent=goodsInfoDto.getPercent();
        this.buyPrice=goodsInfoDto.getBuyPrice();
        this.goodsNum = goodsNum;
    }


    public AddOrdersInfoGoodsDto(CartDto cartDto) {
        this.cartId=cartDto.getCartId();
        this.goodsId = cartDto.getGoodsId();
        this.goodsImg = cartDto.getGoodsImg();
        this.goodsName = cartDto.getGoodsName();
        this.goodsNum =  cartDto.getGoodsNum();
        this.goodsPrice = cartDto.getGoodsPrice();
        this.vipPrice=cartDto.getVipPrice();
        this.groupPrice = cartDto.getGroupPrice();
        this.attributes = cartDto.getAttributes();
        this.courierId = cartDto.getCourierId();
        this.percent=cartDto.getPercent();
        this.buyPrice=cartDto.getBuyPrice();
    }

    public BigDecimal getPercent() {
        return Convert.percentToBigDecimal(this.percent,null);
    }

    public static List<AddOrdersInfoGoodsDto> newsList(List<OrdersGoods> goodsList) {
        List<AddOrdersInfoGoodsDto> ordersInfoGoodsDtoList = Lists.newArrayList();
        AddOrdersInfoGoodsDto goodsDto = null;

        for (OrdersGoods goods : goodsList) {
            goodsDto = new AddOrdersInfoGoodsDto();
            goodsDto.setGoodsId(goods.getGoodsId());
            goodsDto.setOrdersGoodsId(goods.getOrdersGoodsId());
            goodsDto.setGoodsPrice(goods.getGoodsPrice());
            goodsDto.setGoodsNum(goods.getGoodsNum());
            goodsDto.setGoodsName(goods.getGoodsName());
            goodsDto.setGoodsImg(goods.getGoodsImg());
            goodsDto.setGoodsGiftList(goods.getGoodsGiftList());
            goodsDto.setAttributes(goods.getAttributes());
            goodsDto.setIsEvaluate(goods.getEvalStatus().equals(CommonEnum.STATUS_20.getValue()));

            try{
                goodsDto.setPercent(NumberFormat.getPercentInstance().format(goods.getPercent()));
            }catch (Exception e){
                goodsDto.setPercent("");
            }
            goodsDto.setSingleItemRefund(goods.getSingleItemRefund());
            goodsDto.setDiscountStatus(goods.getDiscountStatus());
            goodsDto.setDiscountPrice(goods.getDiscountPrice());
            ordersInfoGoodsDtoList.add(goodsDto);

        }
        return ordersInfoGoodsDtoList;
    }
}
